package de.koller.caldera.input;

import de.koller.caldera.CNode;
import de.koller.oceanFramework.OceanException;
import de.koller.oceanFramework.gl.util.InputGL;
import de.koller.oceanFramework.tree.Child;
import java.util.HashSet;
import java.util.Set;
import org.lwjgl.input.Keyboard;

public class CInput extends CNode {
	@Child private final InputGL input = new InputGL();

	public final Set<IMouseMotionHandler> mouseMotionHandlers = new HashSet<>();
	public final Set<IKeyDownHandler> keyDownHandlers = new HashSet<>();

	private IKeyDownHandler currentKDHandler = null;
	
	public boolean isKeyDown( IKeyDownHandler handler, int key ) {
		if( handler != currentKDHandler )
			throw new OceanException();
		
		return input.isKeyDown( key );
	}
	
	@Override
	protected void update( int elapsedTime ) {
		super.update( elapsedTime );
		
		if( input.isKeyPressed( Keyboard.KEY_F12 ) )
			croot.stop();
		
		for( IMouseMotionHandler h : mouseMotionHandlers )
			h.mouseMoved( input.getMouseDX(), input.getMouseDY() );
		
		for( IKeyDownHandler h : keyDownHandlers ) {
			currentKDHandler = h;
			h.keyDown( this );
		}
		currentKDHandler = null;	
	}
	
}
